home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Delphi Programmer's Power Pack
/
Delphi Volume 1.iso
/
e_to_l
/
fbuilder
/
delphi
/
demos
/
filtrfrm.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1996-09-15
|
7KB
|
285 lines
{* *}
{* FormulaBuilder 1.0 *}
{* YGB Software, Inc. *}
{* Copyight 1995, Clayton Collie *}
{* All Rights Reserved *}
{* *}
{* This unit defines a form TFilterFm which permits the *}
{* user to visually build an expression based on a BDE *}
{* dataset *}
{$F+}
unit Filtrfrm;
interface
uses WinTypes, WinProcs, Classes, Graphics, Forms, Controls, Buttons,
StdCtrls, ExtCtrls,Sysutils,DB,
fbcomp,fbdbcomp,
fbcalc;
type
Datatypeset = Set of Datatypes;
TFilterFm = class(TForm)
CancelBtn: TBitBtn;
HelpBtn: TBitBtn;
Bevel1: TBevel;
FieldListbox: TListBox;
OperatorListbox: TListBox;
FunctionListbox: TListBox;
ExpressionMemo: TMemo;
Bevel2: TBevel;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
GroupBtn: TBitBtn;
BitBtn1: TBitBtn;
procedure FormCreate(Sender: TObject);
procedure FieldListboxClick(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure OperatorListboxClick(Sender: TObject);
procedure FunctionListboxDblClick(Sender: TObject);
procedure GroupBtnClick(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure ExpressionMemoMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
fDataset : TDataset;
fExpression : TCustomDSExpression;
fValidTypes : Datatypeset;
Procedure LoadFieldListbox;
Procedure setExpression(const s : string);
Function getDataset : TDataset;
Procedure SetDataset(const db : TDataset);
Function Evaluate(var vtype : datatypes;var res : integer) : String;
public
{ Public declarations }
Property Dataset : TDataset read getDataset write SetDataset;
end;
var
FilterFm: TFilterFm;
{* Build an expression based on a BDE dataset. The expression type *}
{* must be in the set ValidTypes *}
{* the extra Dataset parameter is a workaround for the fact that *}
{* TCustomDSExpression does not expose its Dataset property *}
Function BuildDSExpression(const theTitle : string;
const ValidTypes : DataTypeSet;
theExpr : TCustomDSExpression;
theDataset : TDataset):boolean;
implementation
uses fbmisc,dialogs;
{$R *.DFM}
{$F+}
Function BuildDSExpression(const theTitle : string;
const ValidTypes : DataTypeSet;
theExpr : TCustomDSExpression;
theDataset : TDataset):boolean;
Var Form1 : TFilterFm;
origExpr : pchar;
oldUsex : boolean;
wasEmpty : boolean;
begin
Application.CreateForm(TFilterFm,Form1);
origExpr := TheExpr.StrFormula;
OldUseX := theExpr.UseExceptions;
TheExpr.UseExceptions := False;
WasEmpty := (OrigExpr = NIL);
TRY
with form1 do
begin
fExpression := theExpr;
Dataset := theDataset;
ExpressionMemo.Lines := TheExpr.Lines;
fValidTypes := ValidTypes;
Caption := theTitle;
result := False;
if ShowModal = mrOk then
Result := true
else
begin
Result := False;
if wasEmpty then TheExpr.Clear;
end;
end;
FINALLY
StrDispose(OrigExpr);
Form1.Free;
END;
theExpr.UseExceptions := OldUsex
end;
Function TFilterFm.Evaluate(var vtype : datatypes;var res : integer) : String;
begin
fExpression.Lines := ExpressionMemo.Lines;
res := FExpression.Status;
if res = EXPR_SUCCESS then
begin
result := FExpression.AsString;
vtype := FExpression.ReturnType;
end;
end; {}
Procedure TFilterFm.setExpression(const s : string);
begin
if fExpression.Formula = s then exit;
fExpression.Formula := s;
expressionMemo.Text := s;
end;
Function TFilterFm.getDataset : TDataset;
begin
result := fDataset;
end;
Procedure TFilterFm.SetDataset(const db : TDataset);
begin
if FieldListBox.items.Count > 0 then
FieldListbox.Clear;
if Assigned(db) then
begin
fDataset := db;
fDataset.GetFieldNames(FieldListbox.Items);
end;
{ LoadFieldListbox; }
end;
(*
procedure TDBExprBuilder.GroupButtonClick(Sender: TObject);
var txt : string;
begin
txt := ExpressionMemo.SelText;
if txt <> '' then
ExpressionMemo.Seltext := '(' + txt + ')';
end;
*)
Procedure TFilterFm.LoadFieldListBox;
begin
if (fDataset = NIL) then exit;
fDataset.GetFieldNames(FieldListbox.Items);
end;
procedure TFilterFm.FormCreate(Sender: TObject);
var thelist : TStringList;
begin
thelist := getFunctionPrototypes(false);
if Assigned(theList) then
begin
FunctionListBox.Items.AddStrings(thelist);
thelist.free;
end;
{ Dispose of global object }
end;
procedure TFilterFm.FieldListboxClick(Sender: TObject);
var
tblname,fldname : string[50];
indx : integer;
begin
indx := FieldListBox.ItemIndex;
if indx = -1 then exit;
FldName := FieldListBox.Items[Indx];
ExpressionMemo.SelText := fldname;
end;
procedure TFilterFm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
if (modalResult = mrCancel) then canclose := true
else
if (ModalResult = mrOk) then
begin
fExpression.Lines := ExpressionMemo.Lines;
if (fExpression.status <> EXPR_SUCCESS) then
begin
canClose := False;
MessageDlg(fExpression.StatusText,mtError,[mbOK],0);
end
else
begin
Canclose := (fExpression.ReturnType in fValidTypes);
if not CanClose then
begin
MessageBeep(mb_iconHand);
MessageDlg(FBCALC.getTypenames(fValidTypes)+' expression expected.', mtInformation,[mbOk],0);
end;
end;
end;
end; {}
procedure TFilterFm.OperatorListboxClick(Sender: TObject);
var
op : string[10];
indx : integer;
begin
indx := OperatorListBox.ItemIndex;
if indx = -1 then exit;
Op := OperatorListBox.Items[Indx];
ExpressionMemo.SelText := ' ' + Op + ' ';
end;
procedure TFilterFm.FunctionListboxDblClick(Sender: TObject);
var fnName : string;
sel : string;
indx : integer;
p : byte;
begin
indx := FunctionListBox.ItemIndex;
if indx = -1 then exit;
FnName := FunctionListBox.Items[Indx];
p := Pos('(',fnName);
if p > 0 then
fnName := Copy(fnName,1,p-1);
fnName := fnName + '( ';
sel := ExpressionMemo.SelText;
if sel <> '' then
ExpressionMemo.SelText := fnName + Sel + ' )'
else
ExpressionMemo.SelText := fnName+' )';
end;
procedure TFilterFm.GroupBtnClick(Sender: TObject);
var txt : string;
begin
txt := ExpressionMemo.SelText;
if txt <> '' then
ExpressionMemo.Seltext := '(' + txt + ')';
end;
procedure TFilterFm.FormActivate(Sender: TObject);
begin
GroupBtn.Enabled := False;
end;
procedure TFilterFm.ExpressionMemoMouseUp(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
GroupBtn.Enabled := ExpressionMemo.SelText <> '';
end;
end.